package com.iring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;

import com.iring.domain.config.prop.UDPProperties;
import com.iring.exception.IringException;
import com.iring.service.common.AllSuiteService;
import com.iring.service.common.DeviceService;
import com.iring.service.pbx.udpsocket.NettySocket;
import com.iring.util.common.IringHandler;
import com.iring.util.common.IringThread;

/**
 * 查询通话记录dao
 *
 * @author wanggan
 */
@SpringBootApplication
public class IringApplication {
    
    private static final Logger logger = LoggerFactory.getLogger(IringApplication.class);
    
    public static void main(String[] args) {
        final ApplicationContext applicationContext =SpringApplication.run(IringApplication.class,args);
        AllSuiteService allSuiteService = applicationContext.getBean(AllSuiteService.class);
        allSuiteService.loadAllSuite();
    
        UDPProperties udpProperties = applicationContext.getBean(UDPProperties.class);
        DeviceService deviceService = applicationContext.getBean(DeviceService.class);
    
        Integer udpPort = udpProperties.getPort();
        Assert.notNull(udpPort, "udp port must not be null");
    
        new Thread(new IringThread(new IringHandler() {
            @Override
            public void execute() {
                try {
                    new NettySocket().startUdp(udpPort, deviceService);
                } catch (InterruptedException e) {
                    logger.info("udp server startup failed");
                    throw new IringException("udp server startup failed");
                }finally {
                    logger.info("new thread listen udp server port at={}", udpPort);
                }
            }
        })).start();
    }
}
